﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using ViCode_LeVi.Data;
using ViCode_LeVi.Controls.ControlsWin;
using System.Windows.Threading;

namespace ViCode_LeVi.Controls
{
    /// <summary>
    /// Interaction logic for VMTabSummary.xaml
    /// </summary>
    public partial class VMTabSummary : UserControl
    {
        public VMTabSummary()
        {
            InitializeComponent();
            this.Loaded += new RoutedEventHandler(VMTabSummary_Loaded);
        }

        void VMTabSummary_Loaded(object sender, RoutedEventArgs e)
        {
            this.SetBinding(FrameworkElement.DataContextProperty, new Binding( ){ Source = RootData.Intance});
            GenData();
            RefreshGridColumnSize_Run();
        }
        List<VMTabSummary_Step> listChild = new List<VMTabSummary_Step>();
        private void GenData()
        {
            RootData data = (RootData)this.DataContext;
            if (data != null)
            {
                Grid_Root.RowDefinitions.Clear();
                Grid_Root.Children.Clear();
                listChild.Clear();
                List<int> listIndex = new List<int>();
                listIndex.Add(0);
                listIndex.Add(1);
                listIndex.Add(2);
                listIndex.Add(3);
                listIndex.Add(4);
                listIndex.Add(5);
                listIndex.Add(6);
                listIndex.Add(7);
                listIndex.Add(9);
                listIndex.Add(8);
                int rowIndex = 0;
                //for (int i = 0; i < data.Steps.Length - 1; i++)
                foreach(var i in listIndex)
                {
                    if (data.Steps[i] != null)
                    {
                        if (Grid_Root.RowDefinitions.Count <= i+1)
                            Grid_Root.RowDefinitions.Add(new RowDefinition());
                        var row = new VMTabSummary_Step();
                        listChild.Add(row);
                        Grid.SetRow(row, rowIndex++);
                        row.SetBinding(FrameworkElement.DataContextProperty, new Binding("DataContext.Steps[" + i.ToString() + "]")
                        {
                            RelativeSource = new RelativeSource
                                (RelativeSourceMode.FindAncestor
                                , typeof(VMTabSummary)
                                , 1)
                        });
                        Grid_Root.Children.Add(row);
                    }
                }
                Grid_Root.UpdateLayout();
                
            }

            windowsFormsHost1.Child = null;
            windowsFormsHost1.Child = new DVContents(TabIDType2.CREA_CONTENTS); 
        }
        DispatcherTimer timer = null; 
        private void RefreshGridColumnSize_Run()
        {
            RefresGridColumnSize();
            ThreadHelper.Runner.RunAfterUIAction(Grid_Root, () =>
            {
                RefresGridColumnSize();
            }, System.Windows.Threading.DispatcherPriority.Background);
            if (timer == null)
            {
                timer = new DispatcherTimer(new TimeSpan(0, 0, 2), DispatcherPriority.Background, (s, e) =>
                    {
                        timer.Stop();
                        RefresGridColumnSize();
                    }, this.Dispatcher);
            }
            timer.Start();
        }
        private void RefresGridColumnSize()
        {
            if (listChild.Count > 0)
            {
                var checkMaxSize = listChild.Select(c => c.ColumnDefinition_ShortName.ActualWidth).Max();
                if (checkMaxSize > 0)
                {
                    foreach (VMTabSummary_Step cln in listChild)
                        cln.ColumnDefinition_ShortName.Width = new GridLength( checkMaxSize, GridUnitType.Pixel);
                }
            }
        }
    }
}
